package finedev.work.vennue;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

import android.app.Activity;
import android.os.AsyncTask;
import android.sax.StartElementListener;
import android.util.Log;

public class HttpManager {
	
	private HttpClient mHttpClient = new DefaultHttpClient();
	
	private HttpPost mHttpPost = null;
	private HashMap<String, String> mHashMapResult = null;
	
	private String mURI = null;
	private HashMap<String, String> mParamHashMap = null;
	
	private String responseStr = null;
	
	public HttpManager(String URI, HashMap<String, String> paramHashMap) {
		// TODO Auto-generated constructor stub
		
		mURI = URI;
		mParamHashMap = paramHashMap;
		new NetworkTask().execute();
	}
	
	private class NetworkTask extends AsyncTask<String, Void, String>{

		@Override
		protected String doInBackground(String... params) {
			// TODO Auto-generated method stub
			try {
				mHttpPost = new HttpPost( new URI( mURI ) );
			} catch (URISyntaxException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			List<NameValuePair> paramList = new ArrayList<NameValuePair>();
			Iterator<String> iterator = mParamHashMap.keySet().iterator();
			
			while ( iterator.hasNext() ) {
				String key = (String) iterator.next();
				Log.i("HttpManager", "key["+key+"] value["+mParamHashMap.get( key )+"]");
				paramList.add(new BasicNameValuePair( key, mParamHashMap.get( key )) );
			}
			
			UrlEncodedFormEntity entity = null;
			try {
				entity = new UrlEncodedFormEntity(paramList);
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	        mHttpPost.setEntity(entity);
	        HttpResponse response = null;
			try {
				response = mHttpClient.execute( mHttpPost );
			} catch (ClientProtocolException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	        HttpEntity resEntity = response.getEntity();
	        try {
				responseStr = EntityUtils.toString(resEntity);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
//			Log.i("Login", responseStr.toString() );
			return responseStr;
		}
		
		protected void onPostExecute(String result) {
			// TODO Auto-generated method stub
			Log.i("Login","onPostExecute");
			try {
				mHashMapResult = new ObjectMapper().readValue( responseStr, HashMap.class );
				Log.i("Login", mHashMapResult.toString() );
			} catch (JsonParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (JsonMappingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
//			super.onPostExecute(result);
		}
	}
}
